<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once '/application/libraries/qiniusdk/autoload.php';
// 引入鉴权类
use Qiniu\Auth;
// 引入上传类
use Qiniu\Storage\UploadManager;
class Deviceapi extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Agent_m');
		$this->load->model('Adminlog_m');
		$this->load->model('Config_m');
		$this->load->model('Device_m');
		$this->load->model('Commission_m');
		$this->load->model('Order_m');
		$this->load->library('api/agentapi_service');
		ini_set('date.timezone','Asia/Shanghai');
		//header('Content-Type: application/json;charset=utf-8');
	}

	public function device_api(){
    	switch ($_SERVER["REQUEST_METHOD"]) {
			case 'GET':
				/**
			     * 通过设备id/设备编号查设备或全查设备
			     */
				$device_id=$this->input->get('device_id');//设备id
				$device_sn=$this->input->get('device_sn');//设备编号
				if($device_id){
					$this->getDeviceByDeviceid();//通过设备id获取单个设备信息
				}else if($device_sn){
					$this->getDeviceByDevicesn();//通过设备编号获取单个设备信息
				}else{
					$this->getDeviceList();//获取设备列表					
				}
				break;				
			case 'POST':
				$this->addDevice();//添加设备
				break;
			case 'PUT':
				$this->updateDeviceUseridByDeviceid();//修改设备信息
				break;
			case 'DELETE':
				# code...
				break;
			default:
				# code...
				break;
		}
	}


	/**
     * 修改设备信息
     * @param device_id     设备id
     * @param user_id     	设备绑定的用户id
     * @param agent1     	设备所属一级代理
     * @param agent2     	设备所属二级代理
     */
	public function updateDeviceUseridByDeviceid(){
		if(IS_PUT){
			$device_id = $this->input->input_stream('device_id');
			$user_id = $this->input->input_stream('user_id');
			$agent1 = $this->input->input_stream('agent1');
			$agent2 = $this->input->input_stream('agent2');
			if(empty($device_id)){
				$data['success'] = error('4008');	    	   	
			    echo _echo($data);
			    exit;
			}
			$this->load->model('Device_m');
			$device_where['device_id']=$device_id;
			if($user_id){
				$data['user_id']=$user_id;
			}
			if($agent1){
				$data['agent1']=$agent1;
			}
			if($agent2){
				$data['agent2']=$agent2;
			}
			$this->Device_m->update($device_where,$data);
			$Device=$this->Device_m->select($device_where);
			$Device_data['success'] = success('4000');	  
			$Device_data['Device'] = $Device;  
		    echo _echo($Device_data);
		    exit;
		}else{
			$data['success'] = error('4008');	    	   	
		    echo _echo($data);
		    exit;
		}
	}



    /**
     * 添加设备 接收设备编号数组
     * @param devicesn_list        设备编号数组
     */

    //多个查询设备状态
    function tianjia(){
	    set_time_limit(0);
		$shebei = array(

'8986031748204892933',
'8986031748204892875',
'8986031748204892906',
'8986031748204892908',
'8986031748204892934',
'8986031748204892807',
'8986031748204892808',
'8986031748204892964',
'8986031748204892961',
'8986031748204892876',
'8986031748204892891',
'8986031748204892809',
'8986031748204892810',
'8986031748204892811',
'8986031748204892812',
'8986031748204892813',
'8986031748204892814',
'8986031748204892815',
'8986031748204892816',
'8986031748204892817',
'8986031748204892818',
'8986031748204892819',
'8986031748204892820',
'8986031748204892821',
'8986031748204892822',
'8986031748204892823',
'8986031748204892824',
'8986031748204892825',
'8986031748204892826',
'8986031748204892827',
'8986031748204892828',
'8986031748204892829',
'8986031748204892830',
'8986031748204892831',
'8986031748204892832',
'8986031748204892833',
'8986031748204892834',
'8986031748204892835',
'8986031748204892836',
'8986031748204892837',
'8986031748204892838',
'8986031748204892839',
'8986031748204892840',
'8986031748204892841',
'8986031748204892842',
'8986031748204892843',
'8986031748204892844',
'8986031748204892845',
'8986031748204892846',
'8986031748204892847',
'8986031748204892848',
'8986031748204892849',
'8986031748204892850',
'8986031748204892851',
'8986031748204892852',
'8986031748204892853',
'8986031748204892854',
'8986031748204892855',
'8986031748204892856',
'8986031748204892857',
'8986031748204892858',
'8986031748204892859',
'8986031748204892860',
'8986031748204892861',
'8986031748204892862',
'8986031748204892863',
'8986031748204892864',
'8986031748204892865',
'8986031748204892866',
'8986031748204892867',
'8986031748204892868',
'8986031748204892869',
'8986031748204892870',
'8986031748204892871',
'8986031748204892872',
'8986031748204892873',
'8986031748204892874',
'8986031748204892877',
'8986031748204892878',
'8986031748204892879',
'8986031748204892880',
'8986031748204892881',
'8986031748204892882',
'8986031748204892883',
'8986031748204892884',
'8986031748204892885',
'8986031748204892886',
'8986031748204892887',
'8986031748204892888',
'8986031748204892889',
'8986031748204892890',
'8986031748204892892',
'8986031748204892893',
'8986031748204892894',
'8986031748204892895',
'8986031748204892896',
'8986031748204892897',
'8986031748204892898',
'8986031748204892899',
'8986031748204892900',
'8986031748204892901',
'8986031748204892902',
'8986031748204892903',
'8986031748204892904',
'8986031748204892905',
'8986031748204892907',
'8986031748204892909',
'8986031748204892910',
'8986031748204892911',
'8986031748204892912',
'8986031748204892913',
'8986031748204892914',
'8986031748204892915',
'8986031748204892916',
'8986031748204892917',
'8986031748204892918',
'8986031748204892919',
'8986031748204892920',
'8986031748204892921',
'8986031748204892922',
'8986031748204892923',
'8986031748204892924',
'8986031748204892925',
'8986031748204892926',
'8986031748204892927',
'8986031748204892928',
'8986031748204892929',
'8986031748204892930',
'8986031748204892931',
'8986031748204892932',
'8986031748204892935',
'8986031748204892936',
'8986031748204892937',
'8986031748204892938',
'8986031748204892939',
'8986031748204892940',
'8986031748204892941',
'8986031748204892942',
'8986031748204892943',
'8986031748204892944',
'8986031748204892945',
'8986031748204892946',
'8986031748204892947',
'8986031748204892948',
'8986031748204892949',
'8986031748204892950',
'8986031748204892951',
'8986031748204892952',
'8986031748204892953',
'8986031748204892954',
'8986031748204892955',
'8986031748204892956',
'8986031748204892957',
'8986031748204892958',
'8986031748204892959',
'8986031748204892960',
'8986031748204892962',
'8986031748204892963',
'8986031748204892965',
'8986031748204892966',
'8986031748204892967',
'8986031748204892968',
'8986031748204892969',
'8986031748204892970',
'8986031748204892971',
'8986031748204892972',
'8986031748204892973',
'8986031748204892974',
'8986031748204892975',
'8986031748204892976',
'8986031748204892977',
'8986031748204892978',
'8986031748204892979',
'8986031748204892980',
'8986031748204892981',
'8986031748204892982',
'8986031748204892983',
'8986031748204892984',
'8986031748204892985',
'8986031748204892986',
'8986031748204892987',
'8986031748204892988',
'8986031748204892989',
'8986031748204892990',
'8986031748204892991',
'8986031748204892992',
'8986031748204892993',
'8986031748204892994',
'8986031748204892995',
'8986031748204892996',
'8986031748204892997',
'8986031748204892998',
'8986031748204892999',
'8986031748204893000',
'8986031748204893001',
'8986031748204893002',
'8986031748204893003',
'8986031748204893004',
'8986031748204893005',
'8986031748204893006',
'8986031748204893007',
'8986031748204893008',
'8986031748204893009',
'8986111822103638667',
'8986111822103638668',
'8986111822103638669',
'8986111822103638670',
'8986111822103638671',
'8986111822103638672',
'8986111822103638673',
'8986111822103638674',
'8986111822103638675',
'8986111822103638676',
'8986111822103638677',
'8986111822103638678',
'8986111822103638679',
'8986111822103638680',
'8986111822103638681',
'8986111822103638682',
'8986111822103638683',
'8986111822103638684',
'8986111822103638685',
'8986111822103638686',
'8986111822103638687',
'8986111822103638688',
'8986111822103638689',
'8986111822103638690',
'8986111822103638691',
'8986111822103638692',
'8986111822103638693',
'8986111822103638694',
'8986111822103638695',
'8986111822103638696',
'8986111822103638697',
'8986111822103638698',
'8986111822103638699',
'8986111822103638700',
'8986111822103638701',
'8986111822103638702',
'8986111822103638703',
'8986111822103638704',
'8986111822103638705',
'8986111822103638706',
'8986111822103638707',
'8986111822103638708',
'8986111822103638709',
'8986111822103638710',
'8986111822103638711',
'8986111822103638712',
'8986111822103638713',
'8986111822103638714',
'8986111822103638715',
'8986111822103638716',
'8986111822103638717',
'8986111822103638718',
'8986111822103638719',
'8986111822103638720',
'8986111822103638721',
'8986111822103638722',
'8986111822103638723',
'8986111822103638724',
'8986111822103638725',
'8986111822103638726',
'8986111822103638727',
'8986111822103638728',
'8986111822103638729',
'8986111822103638730',
'8986111822103638731',
'8986111822103638732',
'8986111822103638733',
'8986111822103638734',
'8986111822103638735',
'8986111822103638736',
'8986111822103638737',
'8986111822103638738',
'8986111822103638739',
'8986111822103638740',
'8986111822103638741',
'8986111822103638742',
'8986111822103638743',
'8986111822103638744',
'8986111822103638745',
'8986111822103638746',
'8986111822103638747',
'8986111822103638748',
'8986111822103638749',
'8986111822103638750',
'8986111822103638751',
'8986111822103638752',
'8986111822103638753',
'8986111822103638754',
'8986111822103638755',
'8986111822103638756',
'8986111822103638757',
'8986111822103638758',
'8986111822103638759',
'8986111822103638760',
'8986111822103638761',
'8986111822103638762',
'8986111822103638763',
'8986111822103638764',
'8986111822103638765',
'8986111822103638766',
'8986111822103638767',
'8986111822103638768',
'8986111822103638769',
'8986111822103638770',
'8986111822103638771',
'8986111822103638772',
'8986111822103638773',
'8986111822103638774',
'8986111822103638775',
'8986111822103638776',
'8986111822103638777',
'8986111822103638778',
'8986111822103638779',
'8986111822103638780',
'8986111822103638781',
'8986111822103638782',
'8986111822103638783',
'8986111822103638784',
'8986111822103638785',
'8986111822103638786',
'8986111822103638787',
'8986111822103638788',
'8986111822103638789',
'8986111822103638790',
'8986111822103638791',
'8986111822103638792',
'8986111822103638793',
'8986111822103638794',
'8986111822103638795',
'8986111822103638796',
'8986111822103638797',
'8986111822103638798',
'8986111822103638799',
'8986111822103638800',
'8986111822103638801',
'8986111822103638802',
'8986111822103638803',
'8986111822103638804',
'8986111822103638805',
'8986111822103638806',
'8986111822103638807',
'8986111822103638808',
'8986111822103638809',
'8986111822103638810',
'8986111822103638811',
'8986111822103638812',
'8986111822103638813',
'8986111822103638814',
'8986111822103638815',
'8986111822103638816',
'8986111822103638817',
'8986111822103638818',
'8986111822103638819',
'8986111822103638820',
'8986111822103638821',
'8986111822103638822',
'8986111822103638823',
'8986111822103638824',
'8986111822103638825',
'8986111822103638826',
'8986111822103638827',
'8986111822103638828',
'8986111822103638829',
'8986111822103638830',
'8986111822103638831',
'8986111822103638832',
'8986111822103638833',
'8986111822103638834',
'8986111822103638835',
'8986111822103638836',
'8986111822103638837',
'8986111822103638838',
'8986111822103638839',
'8986111822103638840',
'8986111822103638841',
'8986111822103638842',
'8986111822103638843',
'8986111822103638844',
'8986111822103638845',
'8986111822103638846',
'8986111822103638847',
'8986111822103638848',
'8986111822103638849',
'8986111822103638850',
'8986111822103638851',
'8986111822103638852',
'8986111822103638853',
'8986111822103638854',
'8986111822103638855',
'8986111822103638856',
'8986111822103638857',
'8986111822103638858',
'8986111822103638859',
'8986111822103638860',
'8986111822103638861',
'8986111822103638862',
'8986111822103638863',
'8986111822103638864',
'8986111822103638865',
'8986111822103638866',
'8986111822103638867',
'8986111822103638868',
'8986111822103638869',
'8986111822103638870',
'8986111822103638871',
'8986111822103638872',
'8986111822103638873',
'8986111822103638874',
'8986111822103638875',
'8986111822103638876',
'8986111822103638877',
'8986111822103638878',
'8986111822103638879',
'8986111822103638880',
'8986111822103638881',
'8986111822103638882',
'8986111822103638883',
'8986111822103638884',
'8986111822103638885',
'8986111822103638886',
'8986111822103638887',
'8986111822103638888',
'8986111822103638889',
'8986111822103638890',
'8986111822103638891',
'8986111822103638892',
'8986111822103638893',
'8986111822103638894',
'8986111822103638895',
'8986111822103638896',
'8986111822103638897',
'8986111822103638898',
'8986111822103638899',
'8986111822103638900',
'8986111822103638901',
'8986111822103638902',
'8986111822103638903',
'8986111822103638904',
'8986111822103638905',
'8986111822103638906',
'8986111822103638907',
'8986111822103638908',
'8986111822103638909',
'8986111822103638910',
'8986111822103638911',
'8986111822103638912',
'8986111822103638913',
'8986111822103638914',
'8986111822103638915',
'8986111822103638916',
'8986111822103638917',
'8986111822103638918',
'8986111822103638919',
'8986111822103638920',
'8986111822103638921',
'8986111822103638922',
'8986111822103638923',
'8986111822103638924',
'8986111822103638925',
'8986111822103638926',
'8986111822103638927',
'8986111822103638928',
'8986111822103638929',
'8986111822103638930',
'8986111822103638931',
'8986111822103638932',
'8986111822103638933',
'8986111822103638934',
'8986111822103638935',
'8986111822103638936',
'8986111822103638937',
'8986111822103638938',
'8986111822103638939',
'8986111822103638940',
'8986111822103638941',
'8986111822103638942',
'8986111822103638943',
'8986111822103638944',
'8986111822103638945',
'8986111822103638946',
'8986111822103638947',
'8986111822103638948',
'8986111822103638949',
'8986111822103638950',
'8986111822103638951',
'8986111822103638952',
'8986111822103638953',
'8986111822103638954',
'8986111822103638955',
'8986111822103638956',
'8986111822103638957',
'8986111822103638958',
'8986111822103638959',
'8986111822103638960',
'8986111822103638961',
'8986111822103638962',
'8986111822103638963',
'8986111822103638964',
'8986111822103638965',
'8986111822103638966',
'8986111822103638967',
'8986111822103638968',
'8986111822103638969',
'8986111822103638970',
'8986111822103638971',
'8986111822103638972',
'8986111822103638973',
'8986111822103638974',
'8986111822103638975',
'8986111822103638976',
'8986111822103638977',
'8986111822103638978',
'8986111822103638979',
'8986111822103638980',
'8986111822103638981',
'8986111822103638982',
'8986111822103638983',
'8986111822103638984',
'8986111822103638985',
'8986111822103638986',
'8986111822103638987',
'8986111822103638988',
'8986111822103638989',
'8986111822103638990',
'8986111822103638991',
'8986111822103638992',
'8986111822103638993',
'8986111822103638994',
'8986111822103638995',
'8986111822103638996',
'8986111822103638997',
'8986111822103638998',
'8986111822103638999',
'8986111822103639000',
'8986111822103639001',
'8986111822103639002',
'8986111822103639003',
'8986111822103639004',
'8986111822103639005',
'8986111822103639006',
'8986111822103639007',
'8986111822103639008',
'8986111822103639009',
'8986111822103639010',
'8986111822103639011',
'8986111822103639012',
'8986111822103639013',
'8986111822103639014',
'8986111822103639015',
'8986111822103639016',
'8986111822103639017',
'8986111822103639018',
'8986111822103639019',
'8986111822103639020',
'8986111822103639021',
'8986111822103639022',
'8986111822103639023',
'8986111822103639024',
'8986111822103639025',
'8986111822103639026',
'8986111822103639027',
'8986111822103639028',
'8986111822103639029',
'8986111822103639030',
'8986111822103639031',
'8986111822103639032',
'8986111822103639033',
'8986111822103639034',
'8986111822103639035',
'8986111822103639036',
'8986111822103639037',
'8986111822103639038',
'8986111822103639039',
'8986111822103639040',
'8986111822103639041',
'8986111822103639042',
'8986111822103639043',
'8986111822103639044',
'8986111822103639045',
'8986111822103639046',
'8986111822103639047',
'8986111822103639048',
'8986111822103639049',
'8986111822103639050',
'8986111822103639051',
'8986111822103639052',
'8986111822103639053',
'8986111822103639054',
'8986111822103639055',
'8986111822103639056',
'8986111822103639057',
'8986111822103639058',
'8986111822103639059',
'8986111822103639060',
'8986111822103639061',
'8986111822103639062',
'8986111822103639063',
'8986111822103639064',
'8986111822103639065',
'8986111822103639066',
'8986111822103639067',
'8986111822103639068',
'8986111822103639069',
'8986111822103639070',
'8986111822103639071',
'8986111822103639072',
'8986111822103639073',
'8986111822103639074',
'8986111822103639075',
'8986111822103639076',
'8986111822103639077',
'8986111822103639078',
'8986111822103639079',
'8986111822103639080',
'8986111822103639081',
'8986111822103639082',
'8986111822103639083',
'8986111822103639084',
'8986111822103639085',
'8986111822103639086',
'8986111822103639087',
'8986111822103639088',
'8986111822103639089',
'8986111822103639090',
'8986111822103639091',
'8986111822103639092',
'8986111822103639093',
'8986111822103639094',
'8986111822103639095',
'8986111822103639096',
'8986111822103639097',
'8986111822103639098',
'8986111822103639099',
'8986111822103639100',
'8986111822103639101',
'8986111822103639102',
'8986111822103639103',
'8986111822103639104',
'8986111822103639105',
'8986111822103639106',
'8986111822103639107',
'8986111822103639108',
'8986111822103639109',
'8986111822103639110',
'8986111822103639111',
'8986111822103639112',
'8986111822103639113',
'8986111822103639114',
'8986111822103639115',
'8986111822103639116',
'8986111822103639117',
'8986111822103639118',
'8986111822103639119',
'8986111822103639120',
'8986111822103639121',
'8986111822103639122',
'8986111822103639123',
'8986111822103639124',
'8986111822103639125',
'8986111822103639126',
'8986111822103639127',
'8986111822103639128',
'8986111822103639129',
'8986111822103639130',
'8986111822103639131',
'8986111822103639132',
'8986111822103639133',
'8986111822103639134',
'8986111822103639135',
'8986111822103639136',
'8986111822103639137',
'8986111822103639138',
'8986111822103639139',
'8986111822103639140',
'8986111822103639141',
'8986111822103639142',
'8986111822103639143',
'8986111822103639144',
'8986111822103639145',
'8986111822103639146',
'8986111822103639147',
'8986111822103639148',
'8986111822103639149',
'8986111822103639150',
'8986111822103639151',
'8986111822103639152',
'8986111822103639153',
'8986111822103639154',
'8986111822103639155',
'8986111822103639156',
'8986111822103639157',
'8986111822103639158',
'8986111822103639159',
'8986111822103639160',
'8986111822103639161',
'8986111822103639162',
'8986111822103639163',
'8986111822103639164',
'8986111822103639165',
'8986111822103639166'

		);
		$data = array();
		//$this->addDevice($shebei);
	 //    foreach ($shebei as $key => $value) {
	    	
		//    //$data[$value] = $this->device_recharge($value);
		// }
	    echo '<pre>';	
	    print_r($data);		
    }




	public function addDevice($shebei){
		//if(IS_POST){
			set_time_limit(3000);
			//$devicesn_list=$this->input->post('devicesn_list');//设备编号数组
			//$devicesn_list=$shebei;//设备编号数组

			// if(empty($devicesn_list)){
			// 	$data['success'] = error('4008');	    	   	
			//     echo _echo($data);
			//     exit;
			// }
			
			//$devicesn_list=json_decode($devicesn_list);
			//$devicesn_list=$devicesn_list;
			//$where['inc_type']='qiniu';
			//$qiniu  = $this->Config_m->getconfig($where);
			//$Device_data['success'] = success('4000');
			foreach ($shebei as $key => $value) {
		    	$this->do_addone($value);
			   //$data[$value] = $this->device_recharge($value);
			}

			//$data = $this->do_addone($shebei);
				  
	    	//echo _echo($Device_data);
	    	exit;
		// }else{
		// 	$data['success'] = error('4008');	    	   	
		//     echo _echo($data);
		//     exit;
		// }
		
	}

	/**
     * 添加设备 接收设备编号数组
     * @param devicesn_list        设备编号数组
     */
	public function add_to_device(){
			$devicesn=$this->input->get('devicesn');//设备编号数组
			//$devicesn_list=json_decode($devicesn);
			$where['inc_type']='qiniu';
			$qiniu  = $this->Config_m->getconfig($where);
			$Device_data['success'] = success('4000');
			$Device_data['device'][]=$this->do_addone($devicesn,$qiniu);  
	    	echo _echo($Device_data);

	    	exit;
	}

	/**
     * 执行添加单个设备 接收设备编号数组
     * @param value        设备编号
     * @param qiniu        七牛参数
     */
	public function do_addone($value){
		$device_sn=$value;

		//$data=$this->chaxun_to_device($device_sn);
		//if($data['result']==70006||$data['result']==70007||$data['result']==0){
			$Device_where = array('device_sn'=>$device_sn); //where
			//查询有没有这个设备
			$Device_old = $this->Device_m->select($Device_where);

			if($Device_old){
				return 0;
		    	exit;
			}else{

				$Device['device_sn'] = $device_sn;
				$Device['addtime'] = date("YmdHis");
				//生成二维码
				//$url='http://'.$_SERVER['SERVER_NAME'].'/mobile/login.html?device_sn='.$device_sn;
				//$img='http://qr.liantu.com/api.php?text='.$url;
	        	//$logo='uploads/logo.png';
	        	//$name='uploads/QCcode/'.$device_sn.'.png';
	        	//$this->mark_photo($img,$device_sn,$logo,$name);

	        	//上传七牛云存储 需要填写你的 Access Key 和 Secret Key
		        //$accessKey = 'MNS70nLctMOm0QVBC6Gq5pk9w7CARLh8YqFppXoH';
		        //$secretKey = 'SQvrFDYKWh6YBtILrXHwFbH3wfKbsF871sEGOXmq';
		        //$accessKey=$qiniu['accessKey'];
        		//$secretKey=$qiniu['secretKey'];
		        // 构建鉴权对象
		        //$auth = new Auth($accessKey, $secretKey);
		        // 要上传的空间
		        //$bucket = 'mifi';
		        //$bucket=$qiniu['bucket'];
		        // 生成上传 Token
		        //$token = $auth->uploadToken($bucket);
		        // 要上传文件的本地路径
		        //$filePath = $name;
		        // 上传到七牛后保存的文件名
		       // $key = $device_sn.'.png';
		        // 初始化 UploadManager 对象并进行文件的上传。
		        //$uploadMgr = new UploadManager();
		        // 调用 UploadManager 的 putFile 方法进行文件的上传。
		        //list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
		        
		        //if(file_exists($name)){
			    //    unlink($name);
			    //}

		        //$Device['QRcode'] = 'http://p1e7rmjtq.bkt.clouddn.com/'.$device_sn.'.png';       
		        //$Device['QRcode'] = $qiniu['qiniuurl'].$device_sn.'.png';	        
				$new_Device = $this->Device_m->insert($Device);//添加设备
				$result['code']=4000;
				$result['msg']="添加成功";
				//$result['device_sn']=$Device['device_sn'];
				//$result['imgurl']=$Device['QRcode'];
				return $result;   
				exit;
			}
		//}else{
		//	$result['code']=4010;
		//	$result['msg']="设备出厂未录入数据";
		//	$result['device_sn']=$device_sn;
		//	return $result;
		//	exit;
		//}
		
	}

	/**
     * 获取全部设备列表
     * @param binduser     是否绑定用户 1已绑  2没绑  没传返还全部
     * @param page         页码
     * @param limit         每页显示数量
     */
	public function getDeviceList(){
		if(IS_GET){
			$binduser=$this->input->get('binduser');
			$page = $this->input->get('page');
	        if(empty($page)){
	        	$page=1;
	        }
	        $limit = $this->input->get('limit');
	        if(empty($limit)){
	        	$limit=5;
	        }
			if(empty($page)||empty($limit)){
				$data['success'] = error('4008');	    	   	
			    echo _echo($data);
			    exit;
			}
			$offset=($page-1)*$limit;
			$where=array();
			if($binduser==1){
				$where='user_id is not null';
			}elseif($binduser==2){
				$where='user_id is null';
			}
			$DeviceList=$this->Device_m->select($where,'device_id desc',"$limit,$offset");
			$Device_data['success'] = success('4000');	  
			$Device_data['DeviceList'] = $DeviceList;  
		    echo _echo($Device_data);
		    exit;
	    }else{
			$data['success'] = error('4008');	    	   	
		    echo _echo($data);
		    exit;
		}
	}

	/**
     * 通过设备id获取单个设备信息
     * @param device_id     设备id
     */
	public function getDeviceByDeviceid(){
		if(IS_GET){
			$device_id=$this->input->get('device_id');
			if(empty($device_id)){
				$data['success'] = error('4008');	    	   	
			    echo _echo($data);
			    exit;
			}
			$where['device_id']=$device_id;
			$Device=$this->Device_m->select($where);
			$Device_data['success'] = success('4000');	  
			$Device_data['Device'] = $Device;  
		    echo _echo($Device_data);
		    exit;
	    }else{
			$data['success'] = error('4008');	    	   	
		    echo _echo($data);
		    exit;
		}
	}


	/**
     * 通过设备编号获取单个设备信息
     * @param device_sn     设备编号
     */
	public function getDeviceByDevicesn(){
		if(IS_GET){

			$this->load->model('Device_m');
			$device_sn=$this->input->get('device_sn');
			

			if(empty($device_sn)){
				$data['success'] = error('4008');	    	   	
			    echo _echo($data);
			    exit;
			}
			$where['device_sn']=$device_sn;
			$Device=$this->Device_m->select($where);
			
			if($Device){
				$Device_data['success'] = success('4000');	  
				$Device_data['Device'] = $Device;  
			    echo _echo($Device_data);
			    exit;
			}else{
				$Device_data['success'] = error('4006');
			    echo _echo($Device_data);
			    exit;
			}			
	    }else{
			$data['success'] = error('4008');	    	   	
		    echo _echo($data);
		    exit;
		}
	}
	/**
     * 通过用户id获取设备信息
     * @param user_id     用户id
     */
	public function getDeviceByuserid(){
		if(IS_GET){
			$sess_user=$this->session->userdata('user');
			
			if($sess_user){				
				
				$where['user_id']=$sess_user[0]['user_id'];
				$Device=$this->Device_m->select($where);
				
				$Device_data['success'] = success('4000');	  
				$Device_data['Device'] = $Device;
			    echo _echo($Device_data);
			    exit;
			}else{
				$data['success'] = error('4008');	    	   	
			    echo _echo($data);
			    exit;
			}
	    }else{
			$data['success'] = error('4008');	    	   	
		    echo _echo($data);
		    exit;
		}
	}


	/**
     * 获取某个代理的设备信息
     * @param agentid     代理id
     */
	public function getDeviceByAgentid(){
		if(IS_GET){
			$agentid=$this->input->get('agentid');//代理id
			if(empty($agentid)){
				$data['success'] = error('4008');	    	   	
			    echo _echo($data);
			    exit;
			}
			$agent=$this->Agent_m->select(array('id'=>$agentid));
		    if(empty($agent)){
		        $data['success'] = error('4011');
		        echo _echo($data);
		        exit;
		    }else{
		    	$agent=$agent[0];
		    }
		    if($agent['agent_level']=='1'){
				$where['agent1']=$agent['id'];
			}elseif($agent['agent_level']=='2'){
				$where['agent2']=$agent['id'];
			}
		    $page = $this->input->get('page');
		    if(empty($page)){
		    	$page=1;
		    }
		    $limit = $this->input->get('limit');
		    if(empty($limit)){
		    	$limit=5;
		    }
		    $jihuo = $this->input->get('jihuo');
		    
		    $data['count']=$this->Device_m->getDevicecount($where);
		    if($jihuo==='0'){
		    	$where['jihuo']=0;
		    }elseif($jihuo==='1') {
		    	$where['jihuo']=1;
		    }
		    $data['success'] = success('4000');
		    $deviceList=$this->Device_m->getList($where,$page,$limit);
		    if($deviceList){
			    foreach ($deviceList as $key => $value) {
		        	//$deviceList[$key]['taocan']=$this->Commission_m->getLastCommission($value['device_sn']);
		        	$deviceList[$key]['taocan']=$this->Order_m->getLastOrder($value['device_sn']);
		        }  
		    }      
		    $data['deviceList']=$deviceList;        
		    echo _echo($data);
		    exit;
	    }else{
			$data['success'] = error('4008');	    	   	
		    echo _echo($data);
		    exit;
		}
	}


	/**
     * 返回七牛云存储token
     */
	public function qiniutoken(){
		$where['inc_type']='qiniu';
		$qiniu  = $this->Config_m->getconfig($where);
		//上传七牛云存储 需要填写你的 Access Key 和 Secret Key
        //$accessKey = 'MNS70nLctMOm0QVBC6Gq5pk9w7CARLh8YqFppXoH';
        //$secretKey = 'SQvrFDYKWh6YBtILrXHwFbH3wfKbsF871sEGOXmq';
        $accessKey=$qiniu['accessKey'];
        $secretKey=$qiniu['secretKey'];
        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        //$bucket = 'mifi';
        $bucket = $qiniu['bucket'];
        // 生成上传 Token
        $token = $auth->uploadToken($bucket);
        echo json_encode($token);
        exit;
	}



	//拼接图片
    private function mark_photo($background,$text,$code,$name){                
        $back=imagecreatefrompng($background);            
        $color=imagecolorallocate($back,0,0,0);           
        $code=imagecreatefrompng($code);           
        $code_w=imagesx($code);
        $code_h=imagesy($code);    
        $font="mobile/font/MSYH.ttf";           
        //imagettftext只认utf8字体，所以用iconv转换
        imagettftext($back,15,0,60,299,$color,$font,$text);        
        //执行合成调整位置
        imagecopyresampled($back, $code, 110,110, 0, 0, 70, 70, $code_w, $code_h);
        imagejpeg($back,$name);           
        imagedestroy($back);
        imagedestroy($code);
    }


    /**
     * post进行设备查询url请求
     * @param string $url
     * @param array $post_data
     */
    function chaxun_to_device($device_id) {
        $str_cookie = isset($ext_params['str_cookie']) ? $ext_params['str_cookie'] : '';$ch = curl_init();//初始化curl
      	$length = strlen($device_id);
		if($length != 16){
			$device_id = "0".$device_id."";
		}

		$get_device_info_url_api_where = array('name' => 'get_device_info_url');
		$get_device_info_uid_api_where = array('name' => 'uid');
		$get_device_info_signkey_api_where = array('name' => 'signkey');

		$get_device_info_url  = $this->Config_m->select($get_device_info_url_api_where);
		$uid  = $this->Config_m->select($get_device_info_uid_api_where);
		$signkey  = $this->Config_m->select($get_device_info_signkey_api_where);
		
        $get_device_info_url = $get_device_info_url[0]['value']; 
       	$uid = $uid[0]['value']; 
       	$signkey = $signkey[0]['value'];
       	
        $url = $get_device_info_url;

        $post_data['id']         = $uid;
        $post_data['ts']         = time();
        $post_data['device_id']  = $device_id;
		
        $sign = hash_hmac('sha512', _echo($post_data), $signkey);
        $header[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
        $header[] = 'sign:'.$sign;
        $header[] = 'uid:'.$uid;
        $postUrl  = $url;
        $curlPost = _echo($post_data);
        
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_COOKIE, $str_cookie);
        //curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_POST, true);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $datas = curl_exec($ch);//运行curl 当前套餐名称
        $request_header = curl_getinfo( $ch, CURLINFO_HEADER_OUT);
        curl_close($ch);
		$data = json_decode($datas, true);
		return $data;  
    }




    /**
     * 批量分配设备给某个代理
     * @param tooken     		代理临时通行证
     * @param agentid     		代理id
     * @param devicesnList     	设备编号列表
     */
    public function deviceToAgent2(){
    	$tooken = $this->input->post('tooken');
    	$agent2 = $this->input->post('agentid');
        $devicesnList = $this->input->post('devicesnList');
        if(empty($tooken)||empty($agent2)||empty($devicesnList)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $agentid=jiemi($tooken);
        $agent=$this->agentapi_service->getAgentById(array('id'=>$agentid));
        if(empty($agent)){
            $data['success'] = error('4011');
            echo _echo($data);
            exit;
        }
        foreach ($devicesnList as $key => $value) {
        	$device=$this->Device_m->select(array('device_sn'=>$value));
        	if($device[0]['agent2']){
        		$result[$value]=false;
        	}else{
        		$this->Device_m->update(array('device_sn'=>$value),array('agent2'=>$agent2,'fptime'=>date('Y-m-d H:i:s')));
        		$result[$value]=true;
        	}        	
        }        
        $Device_data['success'] = success('4000');	  
		$Device_data['result'] = $result;  
	    echo _echo($Device_data);
	    exit;
    }


	/**
     * 某个代理登录后获取自己设备列表
     * @param tooken     代理临时通行证
     * @param page       页码
     * @param limit      每页条数
     * @param jihuo      设备是否激活 0未激活 1激活 不传返回全部
     * @param fenpei     设备是否分配给了二级代理  0未分配 1已分配 不传返回全部
     */
	public function getSelfDeviceList(){
		$tooken = $this->input->get('tooken');
        if(empty($tooken)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $agentid=jiemi($tooken);
        $agent=$this->agentapi_service->getAgentById(array('id'=>$agentid));
        if(empty($agent)){
            $data['success'] = error('4011');
            echo _echo($data);
            exit;
        }
        $whereor=array();
        if($agent['agent_level']=='1'){
			$where['agent1']=$agent['id'];
			$data['count']=$this->Device_m->getDevicecount($where);
        	$data['count']['weifenpei']=$this->Device_m->getDeviceweifenpei($where);
        	//$data['count']['fenpei']=$this->Device_m->getDevicefenpei($where);
		}elseif($agent['agent_level']=='2'){
			$where['agent2']=$agent['id'];
			$data['count']=$this->Device_m->getDevicecount($where);
		}

        $page = $this->input->get('page');
        if(empty($page)){
        	$page=1;
        }
        
        $limit = $this->input->get('limit');
        if(empty($limit)){
        	$limit=10;
        }
        
        $jihuo = $this->input->get('jihuo');
        if($jihuo==='0'){
        	$where['jihuo']=0;
        }elseif($jihuo==='1') {
        	$where['jihuo']=1;
        }
        $fenpei = $this->input->get('fenpei');
        if($fenpei==='0'){
        	$where['jihuo']=0;
        	//$where['agent2']=null;
        }elseif($fenpei==='1') {
        	$where['jihuo']=1;
        	//$whereor="(jihuo = 1 and agent1 = ".$agent['id'].") ";
        }
        $data['success'] = success('4000');

        $deviceList=$this->Device_m->getList($where,$page,$limit,$whereor);
        if($deviceList){
    	    foreach ($deviceList as $key => $value) {
            	//$deviceList[$key]['taocan']=$this->Commission_m->getLastCommission($value['device_sn']);
            	$deviceList[$key]['taocan']=$this->Order_m->getLastOrder($value['device_sn']);
            	if($value['agent2']){
            		$agent2=$this->Agent_m->select(array('id'=>$value['agent2']));
            		$deviceList[$key]['agent2name']=$agent2[0]['agent_name'];
            	}else{
            		$deviceList[$key]['agent2name']='一级代理激活';
            		$deviceList[$key]['fptime']=$value['experience_time'];
            	}
            }  
        }      
        $data['deviceList']=$deviceList;
        echo _echo($data);
        exit;
	}




    //判断是否带正确的登录参数
    function islogin(){
        $tooken = $this->input->get('tooken');
        //没接收到参数
        if(empty($tooken)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }else{
            //解密解不出来
            $adminid=jiemi($tooken);
            if(empty($adminid)){
                $data['success'] = error('4008');
                echo _echo($data);
                exit;
            }
            //没有查到该管理员
            $admin=$this->Admin_m->select(array('admin_id'=>$adminid));
            if(empty($admin)){
                $data['success'] = error('4008');
                echo _echo($data);
                exit;
            }
            return $admin;
        }
    }


	/**
     * 总后台设备列表
     * @param tooken     代理临时通行证
     * @param page       页码
     * @param limit      每页条数
     * @param device_sn   设备编号
     * @param jihuo      设备是否激活  0未激活 1已激活 不传返回全部
     * @param fenpei     设备是否分配给了一级代理  0未分配 1已分配 不传返回全部
     * @param experience 设备是否使用  0未使用 1已使用 不传返回全部
     */
	public function getAllDevice(){
		$this->islogin();
        $page=$this->input->get('page');
        $limit=$this->input->get('limit');
        // $page=1;
        // $limit=10;
        // $fenpei='1';
        if(empty($page)||empty($limit)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $device_sn = $this->input->get('device_sn');
        if($device_sn){
        	$where['device_sn']=$device_sn;
        }
        $jihuo = $this->input->get('jihuo');
        if($jihuo==='0'){
        	$where['jihuo']=0;
        }elseif($jihuo==='1') {
        	$where['jihuo']=1;
        }
        $fenpei = $this->input->get('fenpei');        
        if($fenpei==='0'){
        	$where['agent1']=null;
        }elseif($fenpei==='1') {
        	$where['agent1 > ']=0;
        }
        $experience = $this->input->get('experience');
        if($experience==='0'){
        	$where['experience']=0;
        }elseif($experience==='1') {
        	$where['experience']=1;
        }
        $offset=($page-1)*$limit;
        $orderby=' device_id desc ';
        $deviceList=$this->Device_m->select($where,$orderby,$limit,$offset);
        if($deviceList){
        	$data['deviceList']=$deviceList;
        }
        $data['success'] = success('4000');
        echo _echo($data);
        exit;
	}


	/**
     * 总后台批量分配设备给某个一级代理
     * @param tooken     		临时通行证
     * @param agentid     		代理id
     * @param devicesnList     	设备编号列表
     */
    public function deviceToAgent1(){
    	$admin=$this->islogin();
    	$agent1 = $this->input->post('agentid');
        $devicesnList = $this->input->post('devicesnList');
        if(empty($tooken)||empty($agent1)||empty($devicesnList)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        foreach ($devicesnList as $key => $value) {
        	$device=$this->Device_m->select(array('device_sn'=>$value));
        	if($device[0]['agent1']){
        		$result[$value]=false;
        	}else{
        		$this->Device_m->update(array('device_sn'=>$value),array('agent1'=>$agent1));
        		$result[$value]=true;
        	}        	
        }        
        $Device_data['success'] = success('4000');	  
		$Device_data['result'] = $result;
		//分配设备给一级代理日志
		$log['admin_id']=$admin[0]['admin_id'];
		$log['log_info']='分配设备给一级代理(id:'.$agent1.')'.json_encode($result);
		$log['log_ip']=$_SERVER["REMOTE_ADDR"];
		$log['log_time']=time();
    	$this->Adminlog_m->insert($log);  
	    echo _echo($Device_data);
	    exit;
    }


	/**
     * 总后台设备详情
     * @param tooken     代理临时通行证
     * @param device_sn   设备编号
     */
	public function getDeviceinfoByDevicesn(){
		//$this->islogin();
		$device_sn = $this->input->get('device_sn');
		$device_sn = '866666030863584';
        if($device_sn){
        	$where['device_sn']=$device_sn;
        	$device=$this->Device_m->select($where);
        	$data=$this->chaxun_to_device($device_sn);
        	var_dump($device);
        	var_dump($data);
        }else{
        	$data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
	}

	/**
     * 总后台修改设备详情信息
     * @param tooken get 代理临时通行证
     * @param device_sn  设备编号
     * @param agent1	 一级代理
     * @param agent2	 二级代理
     */
	public function updateDeviceinfoByDevicesn(){
		$admin=$this->islogin();
		$device_sn = $this->input->post('device_sn');
		//$device_sn = '866666030863584';
		$agent1 = $this->input->post('agent1');
		$agent2 = $this->input->post('agent2');
		if($device_sn){
			$Deviceinfo=array();
			if($agent1){
				$Deviceinfo['agent1']=$agent1;
			}
			if($agent2){
				$Deviceinfo['agent2']=$agent2;
			}
        	$where['device_sn']=$device_sn;
        	$this->Device_m->update($where,$Deviceinfo);
        	//修改设备详情信息日志
			$log['admin_id']=$admin[0]['admin_id'];
			$log['log_info']='修改单个设备(sn:'.$device_sn.')'.json_encode($Deviceinfo);
			$log['log_ip']=$_SERVER["REMOTE_ADDR"];
			$log['log_time']=time();
	    	$this->Adminlog_m->insert($log);  
        	$data['success'] = success('4000');
	        echo _echo($data);
	        exit;
        }else{
        	$data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
	}










}


